<template>
    <div class="app-container" style="padding:20px">
        <el-card>
            <FormTopBar :title="title" :flowInsId="form.flowInsId" :taskId="taskId" :businessId="form.id" :billType="form.billType" @submitForm="submitForm"></FormTopBar>
        </el-card>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px" :disabled="readonly">
            <el-card class="top15">
                <el-col :xs="24" :sm="12" :md="8" :lg="8">
                    <el-form-item label="奖惩用户" prop="rpNickName">
                        <el-input style="width: 300px;height: 50px;width: 100%" v-model="form.rpNickName" placeholder="请选择奖惩用户" readonly>
                            <template #suffix>
                                <a type="primary" style="height: 45px;color: #00afff;padding: 0px 10px;" @click="openSecelctOneUser">选择</a>
                            </template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :md="8" :lg="8">
                    <el-form-item label="奖惩金额" prop="amt">
                        <el-input v-model="form.amt" placeholder="请输入奖惩金额"/>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="24" :lg="24">
                    <el-form-item label="奖惩描述" prop="des">
                        <el-input v-model="form.des" type="textarea" :rows="5" placeholder="请输入内容"/>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="24" :lg="24">
                    <el-form-item label="备注" prop="remark">
                        <el-input v-model="form.remark" type="textarea" :rows="5" placeholder="请输入内容"/>
                    </el-form-item>
                </el-col>
            </el-card>
        </el-form>

        <el-dialog :visible.sync="showSelectOneUser" title="选择调岗用户" width="1200px" append-to-body>
            <SelectOneUser @selectedOneUser="selectedOneUser" @closeSelectOneUser="closeSelectOneUser"></SelectOneUser>
        </el-dialog>
    </div>
</template>

<script>
import FlowInfo from "@/views/workflow/flow/flowInfo";
import {listRewardPunish, getRewardPunish, delRewardPunish, addRewardPunish, updateRewardPunish} from "@/api/hrm/rewardPunish";
import FormTopBar from "@/views/workflow/common/formTopBar"
import SelectOneUser from "@/views/common/selectUser/selectOneUser";
import {listPostAll} from "@/api/system/post";
import {getUser} from "@/api/system/user";

export default {
    name: "RewardPunishForm",
    dicts: ['duty_leave_type'],
    components:{FlowInfo,FormTopBar,SelectOneUser},
    data() {
        return {
            title:"",
            // 表单参数
            form: {
                rpNickName:""
            },
            showFlowInfo:false,
            taskId:'',
            readonly:false,
            showSelectOneUser:false,
            // 职位选项
            postOptions: [],
            // 表单校验
            rules: {
                rpNickName: [
                    {required: true, message: "奖惩用户不能为空", trigger: "blur"}
                ]
            },
            showCommitDialog:false,
            commitType:""
        };
    },
    provide(){
        return {
            addCcedList:this.addCcedList
        }
    },
    created() {
        //单据的id主键
        const formId = this.$route.params && this.$route.params.formId;
        //单据的类型
        const billType = this.$route.params && this.$route.params.billType;
        const taskId = this.$route.query && this.$route.query.taskId;
        const readonly = this.$route.query && this.$route.query.readonly;
        if(billType){
            this.form.billType = billType;
            this.setTitle(billType);
        }
        if(taskId){
            this.taskId = taskId;
        }
        if("1"==readonly){
            this.readonly = true;
        }
        if(formId){
            this.form.id = formId;
            this.getDetailData(formId);
        }
    },
    mounted() {
    },
    methods: {
        setTitle(billType){
            if(billType == "staff_punish"){
                this.title="惩罚申请单";
            }else if(billType == "staff_reward"){
                this.title="奖励申请单";
            }
        },
        //  获取单据信息
        getDetailData(id) {
            getRewardPunish(id).then(response => {
                this.form = response.data;
            });
        },
        getPostData(){
            listPostAll().then(res=>{
                this.postOptions = res.data;
            })
        },
        openSecelctOneUser(){
            this.showSelectOneUser = true;
            // this.$refs["form"].clearValidate();
        },
        //选择完一条用户
        selectedOneUser(user){
            console.log("选择一条用户："+JSON.stringify(user))
            this.showSelectOneUser = false;
            this.form.rpUserid = user.userId;
            this.form.rpNickName = user.nickName;
            this.$forceUpdate();
            // this.$refs["form"].clearValidate();
        },
        // 关闭选择一个用户弹窗
        closeSelectOneUser(){
            this.showSelectOneUser = false;
        },
        /** 提交按钮 */
        submitForm() {
            console.log(this.form)
            // this.$refs["form"].clearValidate();
            this.$forceUpdate();
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateRewardPunish(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.closeThisPage();
                        });
                    } else {
                        addRewardPunish(this.form).then(response => {
                            this.$modal.msgSuccess("保存成功");
                            this.closeThisPage();
                        });
                    }
                }
            });
        },
        closeThisPage(){
            if(this.form.billType=="staff_reward"){
                this.$tab.closeOpenPage({ path: "/hrm/rewardPunish/reward",query: {billType:this.form.billType}});
            }else if(this.form.billType=="staff_punish"){
                this.$tab.closeOpenPage({ path: "/hrm/rewardPunish/punish",query: {billType:this.form.billType}});
            }

        },
        // 创建单据时候，选择了抄送人
        addCcedList(ccedList){
            this.form.ccedList = ccedList;
            console.log("回填抄送人")
            console.log(this.form)
        }
    }
};
</script>
<style lang="scss" scoped>

</style>
